// 需要填写的地方已经标出
// 一共4个地方 appID、appsecret、模板ID、微信用户的openid
const db = require('../db/index')
const axios = require('axios');
const moment = require('moment')
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
const axiosPost = function(url, params) {
	return new Promise((resolve, reject) => {
		axios
			.post(url, params)
			.then(res => {
				resolve(res);
			})
			.catch(err => {
				reject(err);
			});
	});
};
const axiosGet = function(url, params) {
	return new Promise((resolve, reject) => {
		axios
			.get(url, {
				params,
			})
			.then(res => {
				resolve(res);
			})
			.catch(err => {
				reject(err);
			});
	});
};

async function getToken() {
	const params = {
		grant_type: 'client_credential',
		appid: 'wx5d081c9ab7206d1a', // 你的appid  1
		secret: 'xxx', // 你的secret 2
	};
	let res = await axiosGet('https://api.weixin.qq.com/cgi-bin/token', params);
	return res.data.access_token;
}
// https://devapi.qweather.com/v7/indices/1d?type=1,3,5,8,9&location=101250401&key=e862605c7f034a34b51ccce6026f2db4
async function getWeather(address) {
	let can = 'https://devapi.qweather.com/v7/weather/3d?location='+address+'&key=e862605c7f034a34b51ccce6026f2db4'
	let res = await axiosGet(can);
	res = JSON.stringify(res.data);
	res = JSON.parse(res)
	return res.daily;
}
async function getqw(address) {
	let can = 'https://devapi.qweather.com/v7/indices/1d?type=1,3,8,9&location='+address+'&key=e862605c7f034a34b51ccce6026f2db4'
	let res = await axiosGet(can);
	res = JSON.stringify(res.data);
	res = JSON.parse(res)
	return res.daily;
}
let index=0
async function templateMessageSend(userToken,template,address,local) {
	const token = await getToken();
	const url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' + token;
	let weatherInfo = await weather(address);
	let qwInfo = await getqw(address);
	let week = await getWeek(weatherInfo.fxDate)
	const params = {
		touser:userToken, // 用户openid 3
		template_id:template, // 模板id 4
		url: 'https://www.bilibili.com/video/BV1TP411n7Dq/?spm_id_from=333.934.0.0&vd_source=e724e386c62989cf4ae2d2f34a02b005',
		topcolor: '#FF0000',
		data: {
			date: {
				value: `${weatherInfo.fxDate},${week}`,
				color: "#173177"
			},
			city: {
				value: local,
				color: "#173177"
			},
			weather: {
				value: weatherInfo.textDay,
				color: "#173177"
			},
			min_temperature: {
				value: `${weatherInfo.tempMin}℃`,
				color: "#173177"
			},
			max_temperature: {
				value: `${weatherInfo.tempMax}℃`,
				color: "#173177"
			},
			shushi: {
				value: `${qwInfo[3].text}`,
				color: "#47d669"
			},
			ziwai: {
				value: `${qwInfo[2].text}`,
				color: "#8b8bd1"
			},
			chuanyi: {
				value: `${qwInfo[1].text}`,
				color: "#46d4d4"
			},
			yundong: {
				value: `${qwInfo[0].text}`,
				color: "#00aa00"
			},
			qita: {
				value: "人间最美，不过鲸落，一念百草生，一念山河成。",
				color: "#0090d8"
			}
		},
	};
	let res = await axiosPost(url, params);
	// 成功做个记录
	if(res.data.errmsg=='ok'){
		index++
	}else{
		console.log('err: ', userToken);
	}
	console.log('res: ', res.data);
	console.log(userToken)
}
// npm i qweather-icon 图标
async function weather(address) {
	let weatherInfo = await getWeather(address)
	for (let val in weatherInfo) {
		weatherInfo = weatherInfo[val]
		break;
	}
	return weatherInfo;
}

function getWeek(date) { // 参数时间戳
	let week = moment(date).day()
	switch (week) {
		case 1:
			return '星期一'
		case 2:
			return '星期二'
		case 3:
			return '星期三'
		case 4:
			return '星期四'
		case 5:
			return '星期五'
		case 6:
			return '星期六'
		case 0:
			return '星期日'
	}
}
const sql =
	"select * from wx_user where is_fa='true'"
// 解析成功会挂载到req
db.query(sql, (err,results) => {
	if (err) return console.log(err)
	if (results.length == 0) return console.log('微信用户数据为空！')
	results = JSON.stringify(results)
	results = JSON.parse(results)
	for (let i in results) {
		templateMessageSend(results[i].wx_name,results[i].wx_template,results[i].wx_address,results[i].wx_local)
	}
})

